package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.*;
import com.itheima.mapper.*;
import com.itheima.service.db.TestSoulService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * java
 */
@Service
public class TestSoulServiceImpl implements TestSoulService {

    @Autowired
    private QuestionsMapper questionsMapper;

    @Autowired
    private UserIsStatusMapper userIsStatusMapper;

    @Autowired
    private QuestionnaireMapper questionnaireMapper;

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private TestResultMapper testResultMapper;

    @Autowired
    private UserNatureMapper userNatureMapper;


    //查询问卷列表
    @Override
    public List<Questions> findQuestionsList() {

        return questionsMapper.selectList(new QueryWrapper<>());

    }


    //根据用户查询测试灵魂解锁信息
    @Override
    public UserIsStatus findStatusByUserId(Long id) {

        QueryWrapper<UserIsStatus> wrapper = new QueryWrapper<>();

        wrapper.eq("user_id", id);

        UserIsStatus userIsStatus = userIsStatusMapper.selectOne(wrapper);

        return userIsStatus;

    }

    //保存用户测灵魂解锁状态信息
    @Override
    public void saveStatus(UserIsStatus userIsStatus) {

        userIsStatusMapper.insert(userIsStatus);

    }

    //查询所有试卷类型
    @Override
    public List<Questionnaire> findQuestionnaire() {

        return questionnaireMapper.selectList(new QueryWrapper<>());

    }

    //查询所有问题选项
    @Override
    public List<Answer> findQuestionsAnswer(Integer id) {

        QueryWrapper<Answer> wrapper = new QueryWrapper<>();

        wrapper.eq("question_id", id);

        return answerMapper.selectList(wrapper);

    }

    //查询选项对应的分数
    @Override
    public Answer findOptionScore(Integer optionId) {

        return answerMapper.selectById(optionId + "");

    }

    //查询分数对应的性格
    @Override
    public Integer findTestResult(Integer score) {

        return testResultMapper.findTestResult(score);
    }

    //查询问题属于哪个级别的试卷
    @Override
    public Questions findPaperIdByQuestionId(String questionId) {

        return questionsMapper.selectById(questionId);

    }

    //更新保存用户结果表
    @Override
    public void updateStatus(UserIsStatus userIsStatus) {

        userIsStatusMapper.updateById(userIsStatus);

    }

    //查询问卷类型结果
    @Override
    public TestResult findResult(Integer id) {

        return testResultMapper.selectById(id);

    }

    //查询维度
    @Override
    public List<UserNature> findUserNature(Long id) {

        QueryWrapper<UserNature> wrapper = new QueryWrapper<>();

        wrapper.eq("user_id", id);

        return userNatureMapper.selectList(wrapper);

    }

    //查询相似的人
    @Override
    public List<UserIsStatus> findScore(Long userId) {

        //创建条件
        QueryWrapper<UserIsStatus> wrapper1 = new QueryWrapper<>();

        //查询自己的结果表
        wrapper1.eq("user_id", userId);
        UserIsStatus userIsStatus = userIsStatusMapper.selectOne(wrapper1);

        //提前创建集合提升等级
        List<UserIsStatus> userIsStatusList = new ArrayList<>();

        //判断中级高级表是否解锁
        if (userIsStatus.getMiddleLevelLock().equals("0") && userIsStatus.getHighLevelLock().equals("0")) {

            //查出三张问卷的分数
            String lowScore = userIsStatus.getLowScore();
            String middleScore = userIsStatus.getMiddleScore();
            String highScore = userIsStatus.getHighScore();

            //创建条件
            QueryWrapper<UserIsStatus> wrapper2 = new QueryWrapper<>();

            wrapper2.ne("user_id", userId);
            wrapper2.eq("low_score", lowScore);
            wrapper2.eq("middle_score", middleScore);
            wrapper2.eq("high_score", highScore);

            //查出相似的人
            userIsStatusList = userIsStatusMapper.selectList(wrapper2);

            //返回
            return userIsStatusList;
        }

        return userIsStatusList;

    }

}
